//
//  MessageListViewController.m
//  SouthChina
//
//  Created by Mr.Y on 14-10-9.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "MessageListViewController.h"
#import "MessageSessionCell.h"
#import "MessageViewController.h"
#import "MessageSessionDao.h"
#import "MessageSession.h"
#import "FriendVerifyViewController.h"
@interface MessageListViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray * messageSessionArr;
    
}
@property(nonatomic,retain) UIView *titlenav;

@end


@implementation MessageListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableView) name:PUBLIC_ATTENTION_BROADCAST object:nil];

        }
    return self;
}

-(void) refreshTableView{
    
  messageSessionArr = [[NSMutableArray alloc] initWithArray:[MessageSessionDao getAllMessageSession]];
    
    NSLog(@"刷新MessageListViewControlle：：%lu",(unsigned long)messageSessionArr.count);
    
    [_messageTableview reloadData];
    //刷新tarbar消息数
    
}

-(void)viewDidUnload
{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:PUBLIC_ATTENTION_BROADCAST
                                                  object:nil];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addTitleBarWithBackBtn:@"信息"];
    //已登录消息列表
    _messageTableview.tableHeaderView.backgroundColor = [UIColor whiteColor];
    _messageTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, TITLE_BAR_HEGIHT+(YNEW), SCREEN_WIDTH, SCREEN_HEIGHT-TABBARHEIGHT-TITLE_BAR_HEGIHT) style:UITableViewStylePlain];
    _messageTableview.delegate = self;
    _messageTableview.dataSource = self;
    _messageTableview.bounces = NO;
    [self.view addSubview:_messageTableview];
    messageSessionArr = [[NSMutableArray alloc] initWithArray:[MessageSessionDao getAllMessageSession]];
    // NSLog(@"%@",messageSessionArr);
    
    [self  setExtraCellLineHidden:_messageTableview];



    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"删除按钮");
    return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
 
        NSLog(@"删除按钮事件");
        MessageSession * messageSession = [messageSessionArr objectAtIndex:indexPath.row];

        [MessageSessionDao deleteMessageSession:messageSession.sess_id];

        [messageSessionArr removeObjectAtIndex:indexPath.row];
        self.editing = YES;
        [_messageTableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [_messageTableview reloadData];
 
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    long count =messageSessionArr.count;
    return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"MessageSessionCell";
    MessageSessionCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
        cell.headImage.layer.masksToBounds = YES;
        cell.headImage.layer.cornerRadius = 25.0;
    }
    MessageSession * messageSession = [messageSessionArr objectAtIndex:indexPath.row];
    
    if (messageSession.badgeNumber>0) {
        cell.badgerNumber.hidden = NO;
        cell.badgeImage.hidden =NO;
        //20151012
        if(messageSession.badgeNumber>99)
           cell.badgerNumber.text = [NSString stringWithFormat:@"%d+",messageSession.badgeNumber];
        else
           cell.badgerNumber.text = [NSString stringWithFormat:@"%d",messageSession.badgeNumber];

    }else{
        cell.badgerNumber.hidden = YES;
        cell.badgeImage.hidden =YES;

    }
    cell.titleLabel.text = [messageSession.title isEqualToString:@""]?@"移动校园门户系统消息":messageSession.title;
    cell.detailLabel.text = [messageSession.text isEqualToString:@""]?@"":messageSession.text;
    cell.timeLabel.text = [messageSession.upate_time stringFromDateEx];
   
    NSURL *url= [NSURL URLWithString: [messageSession.sessionPic hasPrefix:@"http"]?messageSession.sessionPic:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,messageSession.sessionPic]];
    if ([MESSAGE_TYPE_P2P isEqualToString:messageSession.sessionType]) {
        [cell.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    } else if( [MESSAGE_TYPE_P2G isEqualToString:messageSession.sessionType] ){
        //[cell.headImage setImage:[UIImage imageNamed:@"group"]];
        [cell.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
        
    } else if ([MESSAGE_TYPE_FRIEND isEqualToString:messageSession.sessionType]|| [SEND_JOIN_GRP_REQUEST isEqualToString:messageSession.sessionType]|| [APPLY_TO_JOIN_GROUP isEqualToString:messageSession.sessionType]) {
        [cell.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    } else if ([MESSAGE_TYPE_CMD isEqualToString:messageSession.sessionType]) {
        [cell.headImage setImage:[UIImage imageNamed:@"system_message_icon"]];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    MessageSession *session =[messageSessionArr objectAtIndex:indexPath.row];
    if ([@"cmd_friendValidate" isEqualToString:session.sess_id]|| [@"cmd_sendJoinGrpRequest" isEqualToString:session.sess_id] || [@"cmd_applyToJoinGrp" isEqualToString:session.sess_id]) {
        FriendVerifyViewController * friendverify = [[FriendVerifyViewController alloc] init];
        friendverify.session = session;
        [kAppDelegate.iCSDrawerController.navigationController pushViewController:friendverify animated:NO];
    }else
    {
        NSLog(@"弹出公众号聊天框");
        MessageViewController * messageviewcontroller = [[MessageViewController alloc] initWithNibName:nil bundle:nil];
        messageviewcontroller.session = session;
        [kAppDelegate.iCSDrawerController.navigationController pushViewController:messageviewcontroller animated:NO];
    }
    
}


@end
